package com.nsoft.html5npub.utils;

import java.util.List;

import com.nsoft.html5npub.business.BookDeleter;
import com.nsoft.html5npub.business.BookDownloader;
import com.nsoft.html5npub.objects.BookItem;
import com.nsoft.html5npub.objects.BookRange;
import com.nsoft.html5npub.objects.Settings;
import com.nsoft.html5npub.R;
import com.nsoft.html5npub.ViewActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;


@SuppressWarnings("deprecation")
public class BookGridAdapter extends 
							ArrayAdapter<BookItem>{
	private final Context context;
	private List<BookItem> bookList;
	private BookRange bookRange;
	private int[] gridSize;
	
	public BookGridAdapter(Context ctx,
			List<BookItem> list,BookRange range,
			int[] _gridSize){
		super(ctx,R.layout.bookgrid_cell,list);
		this.context=ctx;
		this.bookList=list;
		this.bookRange=range;
		this.gridSize=_gridSize;
	}
	
	@SuppressLint("UseValueOf")
	@Override
	public View getView(int position, View convertView, 
									ViewGroup parent){
		LayoutInflater inflater=(LayoutInflater)context
				.getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		int actualPos=position+bookRange.startIndex;
		BookItem book=this.bookList.get(position);
		
		View view=inflater.inflate(
				R.layout.bookgrid_cell,parent,false);
		ImageView imageView=(ImageView)
				view.findViewById(R.id.bookImg);
		new LoadImageThread(imageView,
					actualPos).execute();
		
		view.setLayoutParams(new GridView.LayoutParams(
						this.gridSize[1],this.gridSize[1]));
		
		ViewGroup.LayoutParams imgLp=
					imageView.getLayoutParams();
		imgLp.width=(int)(this.gridSize[2]*
						Constant.pagesize.RATE);
		imgLp.height=this.gridSize[2];
		
		boolean isBookDownloaded=new Boolean(
				book.getDownloaded().toUpperCase());
		imageView.setAlpha(isBookDownloaded?255:120);
		
		imageView.setOnLongClickListener(new 
					LongClickListener(book,actualPos));
		imageView.setOnClickListener(new 
					BookClickListener(book,actualPos));
		
		return view;
	}
	
	private class BookClickListener implements 
				View.OnClickListener{
		private boolean isBookDownloaded=false;
		private int bookIndex;
		
		@SuppressLint("UseValueOf")
		public BookClickListener(BookItem _book,
							int _bookIndex){
			this.isBookDownloaded=new Boolean(
				_book.getDownloaded().toUpperCase());
			this.bookIndex=_bookIndex;
		}
		
		@Override
		public void onClick(View v) {
			if (this.isBookDownloaded){
				openBook(this.bookIndex);
			}else{
				utils.showInfo(BookGridAdapter.this.context, 
					Constant.DIALOG_ALERT,
					Constant.booklist.BLF_BOOK_NOT_AVAILABLE);
			}
		}
	}
	
	@SuppressLint("UseValueOf")
	private class LongClickListener implements
				View.OnLongClickListener {
		private final int OPTION1_OPEN=0;
		private final int OPTION1_UPDATE=1;
		private final int OPTION1_DELETE=2;
		private final int OPTION2_DOWNLOAD=0;
		
		private BookItem book;
		private int bookIndex;
		private boolean isBookDownloaded;
		
		public LongClickListener(BookItem _book,
							int _bookIndex){
			this.book=_book;
			this.bookIndex=_bookIndex;
			this.isBookDownloaded=new Boolean(
				book.getDownloaded().toUpperCase());
		}
		
		@Override
		public boolean onLongClick(View v) {
			AlertDialog.Builder menu=new AlertDialog.
					Builder(BookGridAdapter.this.context);
			menu.setTitle(R.string.book_popup_title);
			
			if (this.isBookDownloaded){
				menu.setItems(R.array.
					book_select_options_1,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(
								DialogInterface dialog, 
								int index){
							switch(index){
							case OPTION1_OPEN:{
								openBook(bookIndex);
								break;
							}
							case OPTION1_UPDATE:{
								downloadBook(book,bookIndex);
								break;
							}
							case OPTION1_DELETE:{
								deleteBook(book,bookIndex);
								break;
							}
							}
							
						}
					});
			}else{
				menu.setItems(R.array.
					book_select_options_2,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(
								DialogInterface dialog, 
								int index){
							switch(index){
							case OPTION2_DOWNLOAD:{
								downloadBook(book,bookIndex);
								break;
							}
							}
						}
					});			
			}
			if (Settings.isVibrationOn()){
				utils.vibrate(BookGridAdapter.this.context,50);
			}
			menu.create().show();
			return false;
			
		}
		
	}
	
	private void openBook(int bookIndex){
		
		Intent intent=new Intent(
				context,ViewActivity.class);
		intent.putExtra(
				Constant.booklist.BOOK_INDEX_TRANSFER,
				bookIndex);
		context.startActivity(intent);
	}
	
	private void deleteBook(BookItem book,int index){
		final BookItem localBook=book;
		final int bookIndex=index;
		AlertDialog.Builder alert=
				new AlertDialog.Builder(context);
		alert.setIcon(R.drawable.ic_help);
		alert.setTitle(Constant.DIALOG_CONFIRM).
			setMessage(Constant.booklist.BLF_CFM_DOWNLOAD).
			setPositiveButton(Constant.DIALOG_YES,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(
							DialogInterface dialog, 
							int which) {
						try{
							new BookDeleter(
								BookGridAdapter.this.context,
								BookGridAdapter.this,
								localBook,
								bookIndex).
							execute();
						}catch(Exception e){
							Log.v("GridBookArrayAdapter.deleteBook()",
								e.getClass()+": "+e.getMessage());
						}
					}
			}).
			setNegativeButton(Constant.DIALOG_NO,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(
							DialogInterface dialog, 
							int which){
						dialog.cancel();
					}
				}).
			create().show();
	
	}
	
	private void downloadBook(BookItem _book,int _bookIndex){
		final BookItem book=_book;
		final int bookIndex=_bookIndex;
		AlertDialog.Builder alert=
				new AlertDialog.Builder(context);
		alert.setIcon(R.drawable.ic_help);
		alert.setTitle(Constant.DIALOG_CONFIRM).
			setMessage(Constant.booklist.BLF_CFM_DOWNLOAD).
			setPositiveButton(Constant.DIALOG_YES,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(
							DialogInterface dialog, 
							int which) {
						
						if (!utils.isOnline(BookGridAdapter.
										this.context)){
							utils.showError(BookGridAdapter.
								this.context,Constant.
								dataserver.ERROR_NETWORK_TITLE,
								Constant.dataserver.
									ERROR_NETWORK_UNAVAILABLE);
							return;
						}
						
						new BookDownloader(
								BookGridAdapter.this.context,
								BookGridAdapter.this,
								book,
								bookIndex).execute();
					}
			}).
			setNegativeButton(Constant.DIALOG_NO,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(
							DialogInterface dialog, 
							int which){
						dialog.cancel();
					}
				}).
			create().show();
	}

}
